﻿using System;

namespace BuzzX8.Xmpp.StreamElements.Stanza
{
    public class StanzaError : ErrorElement
    {
        private StanzaErrorType errorType;

        public StanzaError(StanzaErrorType errorType) : this(errorType, "")
        {}

        public StanzaError(StanzaErrorType errorType, string errorName) : this(errorType, errorName, "", "")
        {}

        public StanzaError(StanzaErrorType errorType, string errorName, string errorDescription, string descriptionLang) : base(XmppNamespaces.StanzaError)
        {
            ErrorType = errorType;
            ErrorName = errorName;
            ErrorDescription = errorDescription;
            DescriptionLang = descriptionLang;
        }

        public StanzaErrorType ErrorType
        {
            get { return errorType; }
            set
            {
                Attributes["type"] = GetAttributeValueForType(value);
                errorType = value;
            }
        }

        private object GetAttributeValueForType(StanzaErrorType type)
        {
            switch (type)
            {
                case StanzaErrorType.Auth:
                    return "auth";

                case StanzaErrorType.Cancel:
                    return "cancel";

                case StanzaErrorType.Continue:
                    return "continue";

                case StanzaErrorType.Modify:
                    return "modify";

                case StanzaErrorType.Wait:
                    return "wait";

                default:
                    throw new NotImplementedException();
            }
        }

        public string ErrorGeneratorName
        {
            get { return Attributes.ContainsKey("by") ? (string) Attributes["by"] : ""; }
            set { Attributes["by"] = value; }
        }
    }
}
